package com.nnnu.orange.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nnnu.orange.pojo.dto.BooksQueryDto;
import com.nnnu.orange.pojo.entity.Books;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nnnu.orange.pojo.vo.BooksInfoVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 存储每本图书的详细信息 Mapper 接口
 * </p>
 *
 * @author zk
 * @since 2024-11-07
 */
@Mapper
public interface BooksMapper extends BaseMapper<Books> {

    /**
     * 分页查询
     * @param page 分页对象
     * @param queryDto 分页dto
     * @return 分页列表
     */
    Page<BooksInfoVo> selectBooksInfoPage(Page<?> page, BooksQueryDto queryDto);


    List<BooksInfoVo> selectBooksInfoPageNoPagination(BooksQueryDto queryDto);

    // 更新库存时使用乐观锁
    @Update("UPDATE books SET available_copies = available_copies - 1, version = version + 1 WHERE book_id = #{bookId} AND version = #{version}")
    int reduceStockWithVersion(@Param("bookId") Integer bookId, @Param("version") Integer version);
}
